/**
 * Created by chengming on 18/5/24.
 * version 1.0
 */

const BaseHrefAdapter = require('../base/BaseHrefAdapter');

module.exports = class KrIndex extends BaseHrefAdapter{

	parseHtml(html, linkRule){

		var extractJson = html.match(/<script>var props=(.+),locationnal=/);
		var extractObj = JSON.parse(extractJson[1]);

		if(extractObj){
			var newsflash = extractObj['feedPostsLatest|post'];
			let contents = [];
			let rangeText = '';
			for(var i = 0;  i < newsflash.length; i++ ) {

        let title = '【' + newsflash[i].title + '】' + newsflash[i].summary;
        let href = 'https://36kr.com/';
        if (newsflash[i]._type === 'video') {
          href = href + 'video/' + newsflash[i].id
        } else if (newsflash[i]._type === 'post') {
          href = href + 'p/' + newsflash[i].id
      	} else {
					if (newsflash[i].type === 'info_flow') {
            href = href + 'topics/' + newsflash[i].id
          }
				}

				contents.push({
					title:title,
					href:href
				});
				rangeText +=title+href;
			}

			return {
				rangeText: rangeText,
				contents: contents
			};
		}
	}

}